#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, a, b ,ans = INT_MAX;
int mp[1005][1005];
int hh[1005], tt[1005];

int hh2[1005], tt2[1005];
int que[1005][1005];
int que2[1005][1005];
void print(){
    cout <<"mp:\n";
    for(int i = 1; i<= a; i++){
        for(int j = 1; j<= b; j++)cout << mp[i][j] <<" ";
         cout << '\n';
    }
   
}

int solve1()
{
    cin  >> a >> b >> n;
    for(int i = 1; i <= a; i++)
    for(int j = 1; j<= b; j++)cin >> mp[i][j];

    memset(tt,-1,sizeof(tt));
    for(int i = 1; i<= a; i++){
        for(int j = 1; j <= b; j++){
            // 计算到当前行中滑动窗口
            if(hh[i] <= tt[i] && j - que[i][hh[i]] + 1> n)hh[i]++;
            while(hh[i] <= tt[i] && mp[i][tt[i]] < mp[i][j]) tt[i]--;    
            que[i][tt[i]] = j;

            if(hh2[i] <= tt2[i] && j - que[i][hh2[i]] + 1> n)hh2[i]++;
            while(hh2[i] <= tt2[i] && mp[i][tt2[i]] > mp[i][j]) tt2[i]--;    
            que2[i][tt2[i]] = j;

            int Lmin = INT_MAX,Lmax = INT_MIN;
            if(i >= n){
                for(int k = i; k >= i - n + 1; k--){
                    cout <<"i:" << k <<'\n';
                    cout << que2[k][hh2[i]] << " mp[i][j]:" << mp[i][que2[k][hh2[i]]] <<'\n';
                    Lmin = min(Lmin,mp[i][que2[k][hh2[i]]]);
                }

                for(int k = i; k >= i - n + 1; k--){
                    Lmax = max(Lmax,mp[i][que[k][hh[i]]]);
                }

                // cout <<"Lmax:" << Lmax <<" Lmin:" << Lmin << '\n';

                ans = min(ans,Lmax - Lmin);
            }
        }        
    }

    print();


    cout << ans;
   
   return 0;
}


// maxv(i,j,k)=max{maxv(i,j,k-1), maxv(i+1,j+1,k-1), maxv(i+1,j,k-1), maxv(i,j+1,k-1)}

int main(){




    
}